<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小舟畅聊 - 用户登录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1a2980 0%, #26d0ce 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #fff;
        }

        .app-logo {
            text-align: center;
            margin-bottom: 30px;
            animation: fadeIn 1s ease-in-out;
        }

        .app-logo h1 {
            font-size: 2.8rem;
            font-weight: 700;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            letter-spacing: 1px;
            margin-bottom: 10px;
        }

        .app-logo p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .login-container {
            width: 100%;
            max-width: 420px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 12px;
            padding: 40px 35px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            transition: transform 0.3s ease;
        }

        .login-container:hover {
            transform: translateY(-5px);
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }

        .login-header h2 {
            font-size: 1.8rem;
            font-weight: 600;
            margin-bottom: 8px;
        }

        .login-header p {
            color: #666;
            font-size: 0.95rem;
        }

        .input-group {
            margin-bottom: 20px;
            position: relative;
        }

        .input-group i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #777;
            font-size: 18px;
        }

        .input-group input {
            width: 100%;
            padding: 14px 14px 14px 50px;
            border: none;
            border-radius: 8px;
            background: #f5f8fa;
            font-size: 16px;
            color: #333;
            box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }

        .input-group input:focus {
            outline: none;
            border-color: #1a2980;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(26, 41, 128, 0.15);
        }

        .password-container {
            position: relative;
        }

        .toggle-password {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #777;
            font-size: 18px;
        }

        .remember-me {
            display: flex;
            align-items: center;
            gap: 10px;
            color: #555;
            font-size: 15px;
            margin: 15px 0 25px;
        }

        .remember-me input {
            width: 18px;
            height: 18px;
            accent-color: #1a2980;
            cursor: pointer;
        }

        .login-btn {
            background: linear-gradient(to right, #1a2980, #26d0ce);
            color: white;
            border: none;
            padding: 15px;
            border-radius: 8px;
            font-size: 17px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(26, 41, 128, 0.3);
            letter-spacing: 0.5px;
            width: 100%;
        }

        .login-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(26, 41, 128, 0.4);
        }

        .login-btn:active {
            transform: translateY(0);
        }

        .error-message {
            color: #e74c3c;
            text-align: center;
            font-size: 14px;
            min-height: 20px;
            display: block;
            margin: 15px 0;
            padding: 10px;
            background: rgba(231, 76, 60, 0.1);
            border-radius: 6px;
        }

        .footer-links {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }

        .footer-links a {
            color: #1a2980;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.3s ease;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .footer-links a:hover {
            color: #26d0ce;
            text-decoration: underline;
        }

        .copyright {
            margin-top: 30px;
            text-align: center;
            color: rgba(255, 255, 255, 0.85);
            font-size: 14px;
            max-width: 600px;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
            }

            .app-logo h1 {
                font-size: 2.2rem;
            }

            .input-group input {
                padding: 12px 12px 12px 45px;
            }
        }
    </style>
</head>
<body>
<div class="app-logo">
    <h1>小舟畅聊</h1>
    <p>让沟通更简单</p>
</div>

<div class="login-container">
    <div class="login-header">
        <h2>用户登录</h2>
        <p>请输入您的账号信息</p>
    </div>

    <form id="loginForm">
        <div class="input-group">
            <i class="fas fa-user"></i>
            <input
                    type="text"
                    id="username"
                    placeholder="请输入用户名"
                    required
                    autocomplete="username"
            >
        </div>

        <div class="input-group password-container">
            <i class="fas fa-lock"></i>
            <input
                    type="password"
                    id="password"
                    placeholder="请输入密码"
                    required
                    autocomplete="current-password"
            >
            <span class="toggle-password" id="togglePassword">
                    <i class="fas fa-eye"></i>
                </span>
        </div>

        <div class="remember-me">
            <input type="checkbox" id="rememberMe">
            <label for="rememberMe">记住密码</label>
        </div>

        <button type="button" class="login-btn" id="loginBtn">
            <i class="fas fa-sign-in-alt"></i> 登录
        </button>

        <div class="error-message" id="errorMsg"></div>

        <div class="footer-links">
            <a href="register.html">
                <i class="fas fa-user-plus"></i> 注册新账号
            </a>
            <a href="#">
                <i class="fas fa-key"></i> 忘记密码?
            </a>
        </div>
    </form>
</div>

<div class="copyright">
    <p>© 2025 小舟畅聊 - 专业沟通平台</p>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 获取DOM元素
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const rememberMeCheckbox = document.getElementById('rememberMe');
        const loginBtn = document.getElementById('loginBtn');
        const errorMsg = document.getElementById('errorMsg');
        const togglePassword = document.getElementById('togglePassword');

        // 检查是否有保存的凭证
        checkSavedCredentials();

        // 密码可见性切换
        togglePassword.addEventListener('click', function() {
            const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
            passwordInput.setAttribute('type', type);
            this.innerHTML = type === 'password' ?
                '<i class="fas fa-eye"></i>' :
                '<i class="fas fa-eye-slash"></i>';
        });

        // 登录按钮事件
        loginBtn.addEventListener('click', login);

        // 按Enter键登录
        passwordInput.addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                login();
            }
        });

        // 登录函数
        function login() {
            // 获取输入值
            const username = usernameInput.value.trim();
            const password = passwordInput.value.trim();
            const rememberMe = rememberMeCheckbox.checked;

            // 验证输入
            if (!username || !password) {
                showError('用户名和密码不能为空！');
                return;
            }

            // 显示加载状态
            loginBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 登录中...';
            loginBtn.disabled = true;

            // 准备请求数据
            const requestData = {
                username: username,
                password: password,
                rememberMe: rememberMe
            };

            // 发送登录请求
            fetch('user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(requestData)
            })
                .then(response => response.json())
                .then(data => {
                    if (data.flag) {
                        // 登录成功
                        showSuccess('登录成功，正在跳转...');
                        setTimeout(() => {
                            window.location.href = "main.html";
                        }, 1000);
                    } else {
                        // 登录失败
                        showError(data.message || '登录失败，用户名或密码错误');
                        resetLoginButton();
                    }
                })
                .catch(error => {
                    console.error('登录请求失败:', error);
                    showError('网络错误，请稍后重试');
                    resetLoginButton();
                });
        }

        // 检查保存的凭证
        function checkSavedCredentials() {
            const username = getCookie('rememberedUser');
            const password = getCookie('rememberedPass');

            if (username && password) {
                usernameInput.value = username;
                passwordInput.value = password;
                rememberMeCheckbox.checked = true;
            }
        }

        // 获取Cookie
        function getCookie(name) {
            const nameEQ = name + "=";
            const ca = document.cookie.split(';');
            for (let i = 0; i < ca.length; i++) {
                let c = ca[i];
                while (c.charAt(0) === ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

        // 显示错误消息
        function showError(message) {
            errorMsg.textContent = message;
            errorMsg.style.display = 'block';
        }

        // 显示成功消息
        function showSuccess(message) {
            errorMsg.textContent = message;
            errorMsg.style.color = '#2ecc71';
            errorMsg.style.background = 'rgba(46, 204, 113, 0.1)';
            errorMsg.style.display = 'block';
        }

        // 重置登录按钮状态
        function resetLoginButton() {
            loginBtn.innerHTML = '<i class="fas fa-sign-in-alt"></i> 登录';
            loginBtn.disabled = false;
        }
    });
</script>
</body>
</html>